﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMemoryProfiler
{

    /// <summary>
    /// Debugger events which does cause a breakpoint when one of the following events do happen
    /// </summary>
    [Flags]
    public enum DebuggerEvents
    {
        /// <summary>
        /// No event to be used in a event filter mask
        /// </summary>
        None = 1 << 0,
        /// <summary>
        /// Debug.Assert was fired
        /// </summary>
        Assert = 1 << 1,
        /// <summary>
        /// AccessViolation
        /// </summary>
        AccessViolation = 1 << 2,
        /// <summary>
        /// The method you did call expected data to be aligned (usually 16 byte)
        /// </summary>
        DataMisaligned = 1 << 3,
        /// <summary>
        /// DivisionByZero
        /// </summary>
        DivisionByZero = 1 << 4,
        /// <summary>
        /// C++ Exception
        /// </summary>
        CppException = 1 << 5,
        /// <summary>
        /// Some read only page was tried to write to
        /// </summary>
        GuardPageViolation = 1 << 6,
        /// <summary>
        /// Process did not recognize this opcode as valid 
        /// </summary>
        IllegalInstruction = 1 << 7,
        /// <summary>
        /// Not sure when this will fire
        /// </summary>
        IntegerOverflow = 1 << 8,
        /// <summary>
        /// When memory from the paged file is swaped back from disc to memory and there was a problem with the disc
        /// </summary>
        InPageIOError = 1 << 9,
        /// <summary>
        /// 
        /// </summary>
        InvalidSystemCall = 1 << 10,
        /// <summary>
        /// 
        /// </summary>
        InvalidLockSequence = 1 << 11,
        /// <summary>
        /// Recursive method call which did exahust the stack memory 
        /// </summary>
        StackOverflow = 1 << 12,
        /// <summary>
        /// Not sure when this will trigger. But the debugger can detect certain causes of stack imbalances
        /// </summary>
        StackBufferOverflow = 1 << 13,
        /// <summary>
        /// Windows does fire this when it detects that an application did not respond anymore (mostly due to a blocked UI message pump)
        /// </summary>
        ApplicationHang = 1 << 14,
        /// <summary>
        /// .NET Exception
        /// </summary>
        ClrException = 1 << 15,
        /// <summary>
        /// Load Module event
        /// </summary>
        LoadModule = 1 << 16,

        /// <summary>
        /// Default ignore mask which does ignore C++, .NET and AccessViolation Exceptions
        /// </summary>
        Default = AccessViolation | CppException | ClrException,
        
    }

    class DebuggerEventMapper
    {
        public static Dictionary<DebuggerEvents,string> Mapper = new Dictionary<DebuggerEvents,string>
        {
            { DebuggerEvents.Assert, "asrt"},
            { DebuggerEvents.AccessViolation, "av"},
            { DebuggerEvents.DataMisaligned, "dm"},
            { DebuggerEvents.DivisionByZero, "dz"},
            { DebuggerEvents.CppException, "eh"},
            { DebuggerEvents.GuardPageViolation, "gp"},
            { DebuggerEvents.IllegalInstruction, "ii" },
            { DebuggerEvents.IntegerOverflow, "iov"},
            { DebuggerEvents.InPageIOError, "ip"},
            { DebuggerEvents.InvalidSystemCall, "isc"},
            { DebuggerEvents.InvalidLockSequence, "lsq"},
            { DebuggerEvents.StackOverflow, "sov"},
            { DebuggerEvents.StackBufferOverflow, "sbo"},
            { DebuggerEvents.ApplicationHang, "aph" },
            { DebuggerEvents.ClrException, "clr" },
            { DebuggerEvents.LoadModule, "ld"},
        };
    }
}
